//
//  MOCaiPuViewCell.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/10/22.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOCaiPuViewCell.h"
#import "MOCaiPuFoodViewCell.h"

@interface MOCaiPuViewCell ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UIButton *nameButton;

@property(nonatomic,strong)UIButton *rightButton;

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation MOCaiPuViewCell

+(instancetype)cellWithTableView:(UITableView *)tableView andIndexPath:(NSIndexPath *)indexPath
{
    //NSString *ID = [NSString stringWithFormat:@"Cell%ld%ld",(long)[indexPath section],(long)[indexPath row]];
    NSString *ID = @"MOCaiPuViewCell";
    MOCaiPuViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil){
        cell = [[MOCaiPuViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell creatView];
    }
    return cell;
}


-(void)creatView
{
    UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 54)];
    [backButton addTarget:self action:@selector(clickFoodType) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:backButton];
    
    self.nameButton = [[UIButton alloc]initWithFrame:CGRectMake(25, 0, SCREEN_WIDTH - 25 - 15 - 10 - 20, 54)];
    self.nameButton.titleLabel.font = FONT_SIZE(15);
    [self.nameButton setTitleColor:KBlackColor_2A2A2A forState:UIControlStateNormal];
    [self.nameButton setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentLeft)];
    self.nameButton.userInteractionEnabled = NO;
    [backButton addSubview:self.nameButton];
    
    self.rightButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-20-15, 0, 20, 54)];
    self.rightButton.titleLabel.font = FONT_SIZE(15);
    [self.rightButton setTitleColor:KBlackColor_2A2A2A forState:UIControlStateNormal];
    self.rightButton.userInteractionEnabled = NO;
    [backButton addSubview:self.rightButton];
    
    [self addSubview:self.tableView];
}

-(void)clickFoodType
{
    if (self.clickFoodTypeBlock) {
        self.clickFoodTypeBlock();
    }
}

-(void)setModel:(MOCaiPuModel *)model
{
    _model = model;
    switch ([model.typeStr integerValue]) {
        case 1:
            [self.nameButton setImage:IMAGE(@"can_zao") forState:UIControlStateNormal];
            [self.nameButton setTitle:@" 早餐" forState:UIControlStateNormal];
            break;
        case 2:
            [self.nameButton setImage:IMAGE(@"can_wu") forState:UIControlStateNormal];
            [self.nameButton setTitle:@" 午餐" forState:UIControlStateNormal];
            break;
        case 3:
            [self.nameButton setImage:IMAGE(@"can_wan") forState:UIControlStateNormal];
            [self.nameButton setTitle:@" 晚餐" forState:UIControlStateNormal];
            break;
        case 4:
            [self.nameButton setImage:IMAGE(@"can_other") forState:UIControlStateNormal];
            [self.nameButton setTitle:@" 其他" forState:UIControlStateNormal];
            break;
            
        default:
            break;
    }
    
    if ([model.open integerValue]==1) {
        [self.rightButton setImage:IMAGE(@"can_top") forState:UIControlStateNormal];
        self.tableView.height = model.arr.count * 54;
    }else{
        [self.rightButton setImage:IMAGE(@"can_right") forState:UIControlStateNormal];
        self.tableView.height = 0;
    }
    self.dataArray = [NSMutableArray arrayWithArray:model.arr];
    [self.tableView reloadData];
}

+ (CGFloat)heightForMOCaiPuViewCellAndModel:(MOCaiPuModel *)model
{
    if ([model.open integerValue]==1) {
        return 54+54*model.arr.count;
    }else{
        return 54;
    }
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    if ([self.model.open integerValue]==1) {
        return self.dataArray.count;
    }else{
        return 0;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MOCaiPuFoodViewCell *cell = [MOCaiPuFoodViewCell cellWithTableView:tableView andIndexPath:indexPath];
    cell.nameStr = self.dataArray[indexPath.section];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 54;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0001;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}
#pragma mark - 设置顶头分割线
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        if (@available(iOS 8.0, *)) {
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        if (@available(iOS 8.0, *)) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}


#pragma mark -懒加载
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 54, SCREEN_WIDTH, 1) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.scrollEnabled = NO;
        _tableView.separatorColor = KGrayColor_f8f8f8;
        _tableView.backgroundColor = KGrayColor_f8f8f8;
    }
    return _tableView;
}




@end
